home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / batch / Randomcopy0_91.lha / RandomCopy / rc.e < prev   
Text File  |  1994-11-21  |  8KB  |  338 lines

  1.  
  2. /*      Randomcopy.e      COPYRIGHT 1993 by Chad Randall (crandall)  */
  3.  
  4.  
  5. MODULE 'dos/dos','dos/dosextens','dos/dosasl','exec/tasks'
  6.  
  7.  
  8. /* Sorry, I like global variables.  Could be from 10 years of Basic/asm...*/
  9.  
  10. DEF source[150]:STRING
  11. DEF dest[150]:STRING
  12. DEF work[150]:STRING
  13. DEF work2[150]:STRING
  14. DEF argarray[16]:LIST
  15.  
  16. DEF userndfile=FALSE,fha=0,bb=0
  17. DEF i,t,quietflag,cloneflag,seconds,micros,random,dd,ticksflag,waittime,longbreak
  18. DEF rdarg,noreqflag,minwait,maxwait,buffersize=512
  19.  
  20. DEF    workfh=NIL:PTR TO filehandle,
  21.         destfh=NIL:PTR TO filehandle
  22. DEF apath=NIL:PTR TO anchorpath
  23.  
  24. DEF    datestamp=NIL:PTR TO datestamp,comment[100]:STRING,condom
  25.  
  26. CONST AOK=69            /*  GRIN  8->  */
  27. CONST NOT_AOK=666  /*  Just a joke... or is it?  >:">  */
  28.  
  29. ENUM ERR_NONE,ERR_INCORRECT,ERR_DOS,ERR_NOSOURCE,ERR_NODEST,ERR_MINMIN,ERR_MINMAX,
  30.             ERR_MAXMIN,ERR_MAXMAX,ERR_MIN_MORE_MAX,ERR_BUFFER_TO_SMALL,ERR_NOMEM,
  31.             ERR_CTRLC
  32.  
  33. RAISE ERR_CTRLC IF CtrlC ()=TRUE
  34.  
  35. PROC main() HANDLE
  36.  
  37.     IF KickVersion(37)=NIL
  38.         WriteF('\nGet OS2.x (maybe a nice A1200?)\n')
  39.         CleanUp(21)
  40.     ENDIF
  41.  
  42.     IF FreeStack()<3000                         /*I can't seem to test this. E seems to*/
  43.         WriteF('Not enough free stack space.\n')           /* have it's own stack?*/
  44.         CleanUp(21)
  45.     ENDIF
  46.  
  47.     CurrentTime({seconds},{micros})
  48.     
  49.     random:=RndQ(seconds)
  50.  
  51.     /* An attempt to get a pseudo-randomized number! */
  52.     /*Let's hope this doesn't slow down too bad on non '30 machines...*/
  53.  
  54.     CurrentTime({seconds},{micros})
  55.  
  56.     FOR i:=0 TO micros/1000
  57.         dd:=Rnd(i)
  58.     ENDFOR
  59.  
  60.     rdarg:=ReadArgs('FROM/A,TO/A,MIN=MINWAIT/N,MAX=MAXWAIT/N,UT=USETICKS/S,LB=LONGBREAK/S,QUIET/S,CLONE/S,NOREQ/S,BUF=BUFFER/K/N,RNDFILE/S,ABOUT/S',argarray,0)
  61.  
  62.     IF rdarg>0
  63.         IF argarray[0]<>NIL
  64.             StrCopy(source,argarray[0],ALL)
  65.         ENDIF
  66.         IF argarray[1]<>NIL
  67.             StrCopy(dest,argarray[1],ALL)
  68.         ENDIF
  69.         IF argarray[2]<>NIL
  70.             minwait:=argarray[2]
  71.             minwait:=^minwait
  72.             IF (minwait<1) THEN Raise(ERR_MINMIN)
  73.             IF (minwait>9999) THEN Raise(ERR_MINMAX)
  74.         ENDIF
  75.         IF argarray[3]<>NIL
  76.             maxwait:=argarray[3]
  77.             maxwait:=^maxwait
  78.             IF (maxwait<2) THEN Raise(ERR_MAXMIN)
  79.             IF (maxwait>10000) THEN Raise(ERR_MAXMAX)
  80.             IF (maxwait<=minwait) THEN Raise(ERR_MIN_MORE_MAX)
  81.         ENDIF
  82.         IF argarray[4]<>NIL
  83.             ticksflag:=TRUE
  84.         ENDIF
  85.         IF argarray[5]<>NIL
  86.             longbreak:=TRUE
  87.         ENDIF
  88.         IF argarray[6]<>NIL
  89.             quietflag:=TRUE
  90.         ENDIF
  91.         IF argarray[7]<>NIL
  92.             cloneflag:=TRUE
  93.         ENDIF
  94.         IF argarray[8]<>NIL
  95.             noreqflag:=TRUE
  96.         ENDIF
  97.         IF argarray[9]<>NIL
  98.             buffersize:=argarray[9]
  99.             buffersize:=^buffersize
  100.             IF (buffersize=0 OR buffersize>65000)
  101.                 Raise(ERR_BUFFER_TO_SMALL)
  102.             ENDIF
  103.         ENDIF
  104.         IF argarray[10]<>NIL
  105.             userndfile:=TRUE
  106.         ENDIF
  107.         IF argarray[11]<>NIL
  108.             instructions()
  109.         ENDIF
  110.         FreeArgs(rdarg)
  111.     ELSE
  112.         Raise(ERR_DOS)
  113.     ENDIF
  114.  
  115.     CtrlC()
  116.  
  117.     bb:=[0,0,0,0,0,0,0]:LONG
  118.     IF (userndfile)
  119.         fha:=Open('s:randomcopy.seed',MODE_OLDFILE)
  120.         IF fha
  121.             Read(fha,bb,4)
  122.             random:=RndQ(Long(bb))
  123.             Close(fha)
  124.         ENDIF    
  125.     ENDIF
  126.     docopy()
  127.     IF (userndfile)
  128.         fha:=Open('s:randomcopy.seed',MODE_NEWFILE)
  129.         IF fha
  130.             random:=RndQ(random);random:=RndQ(random);random:=RndQ(random)
  131.             PutLong(bb,random)
  132.             Write(fha,bb,4)
  133.             Close(fha)
  134.         ENDIF    
  135.     ENDIF    
  136.     IF (minwait OR maxwait)
  137.         IF (maxwait<minwait)
  138.             maxwait:=minwait
  139.         ENDIF                
  140.         IF ticksflag=NIL
  141.             minwait:=minwait*50
  142.             maxwait:=maxwait*50
  143.         ENDIF
  144.         WHILE TRUE<>FALSE             /* Will be broken by Ctrl-C ONLY! */
  145.             waittime:=maxwait-minwait+1
  146.             waittime:=Rnd(waittime)
  147.             waittime:=waittime+minwait
  148.             CtrlC()
  149.             IF ((waittime<100) OR (longbreak))
  150.                 Delay(waittime)
  151.             ELSE
  152.                 t:=waittime/100
  153.                 FOR i:=0 TO t
  154.                     CtrlC()
  155.                     Delay(100)
  156.                 ENDFOR
  157.                 t:=waittime-(t*100)
  158.                 Delay(t)
  159.             ENDIF
  160.             docopy()
  161.         ENDWHILE
  162.     ENDIF
  163.  
  164. EXCEPT
  165.  
  166.     SELECT exception
  167.         CASE ERR_INCORRECT
  168.             quietprint('incorrect arguments')
  169.         CASE ERR_DOS
  170.             IF quietflag=NIL
  171.                 PrintFault(IoErr(),'randomcopy')
  172.             ENDIF
  173.         CASE ERR_MINMIN
  174.             quietprint('minimum wait is too small')
  175.         CASE ERR_MINMAX
  176.             quietprint('minimum wait is too large')
  177.         CASE ERR_MAXMIN
  178.             quietprint('maximum wait is too small')
  179.         CASE ERR_MAXMAX
  180.             quietprint('maximum wait is too large')
  181.         CASE ERR_MIN_MORE_MAX
  182.             quietprint('minimum is greater than maximum')
  183.         CASE ERR_BUFFER_TO_SMALL
  184.             quietprint('illegal buffersize size')
  185.         CASE ERR_NOMEM
  186.             quietprint('not enough memory available')
  187.         CASE ERR_CTRLC
  188.             quietprint('***Break')
  189.     ENDSELECT
  190.     IF (apath) THEN MatchEnd(apath);apath:=NIL
  191.     IF (destfh) THEN Close(destfh);destfh:=NIL    /* Try to make sure we unlock all */
  192.     IF (workfh) THEN Close(workfh);workfh:=NIL
  193.     CleanUp(11)
  194.  
  195. ENDPROC
  196.  
  197. PROC quietprint(str)
  198.     IF quietflag=NIL
  199.         WriteF('randomcopy: \s\n',str)
  200.     ENDIF
  201. ENDPROC
  202.  
  203. PROC docopy()
  204.     DEF fileinfo=NIL:PTR TO fileinfoblock
  205.     DEF    achain=NIL:PTR TO achain
  206.     DEF err,pathlen,filestart,first,chance=1
  207.     DEF    newdate=NIL:PTR TO datestamp
  208.  
  209.     filestart:=FilePart(source)
  210.     pathlen:=filestart-source
  211.     IF (pathlen)
  212.         StrCopy(work,source,pathlen)
  213.     ELSE
  214.         StrCopy(work,'',ALL)
  215.     ENDIF
  216.  
  217.     apath:=New(SIZEOF anchorpath)
  218.     datestamp:=New(SIZEOF datestamp)
  219.  
  220. /*    fileinfo:=New(SIZEOF fileinfoblock) */
  221.  
  222.     err:=NIL;first:=FALSE
  223.     WHILE err=NIL
  224.         CtrlC()
  225.         IF first=FALSE
  226.             err:=MatchFirst(source,apath)
  227.             first:=TRUE
  228.         ELSE
  229.             err:=MatchNext(apath)
  230.         ENDIF
  231.         IF (err)
  232.             IF (err=ERROR_NO_MORE_ENTRIES)
  233.                 IF chance>1
  234.                     copyfile()
  235.                 ELSE
  236.                     Raise(ERR_DOS)
  237.                 ENDIF
  238.             ELSE
  239.                 Raise(ERR_DOS)
  240.             ENDIF
  241.         ELSE
  242.             achain:=apath.last
  243.             IF (achain)
  244.                 fileinfo:=achain.info
  245.                 IF (fileinfo.direntrytype)<0
  246.                     IF (fileinfo)
  247.                         IF(Rnd(chance))=NIL
  248.                             StrCopy(work2,fileinfo.filename,ALL)
  249.                             IF cloneflag
  250.                                 StrCopy(comment,fileinfo.comment,ALL)
  251.                                 condom:=fileinfo.protection
  252.                                 newdate:=fileinfo.datestamp
  253.                                 datestamp.days:=newdate.days
  254.                                 datestamp.minute:=newdate.minute
  255.                                 datestamp.tick:=newdate.tick
  256.                             ENDIF
  257.                         ENDIF
  258.                         chance:=chance+1
  259.                     ENDIF
  260.                 ENDIF
  261.             ENDIF
  262.         ENDIF
  263.     ENDWHILE
  264.  
  265.     MatchEnd(apath);apath:=NIL
  266.  
  267. ENDPROC
  268.  
  269. PROC copyfile()
  270.     DEF    filesize
  271.     DEF cont=FALSE,numbytes,buffer,numbytes2
  272.  
  273.     buffer:=New(buffersize)
  274.     IF (buffer=0)
  275.         Raise(ERR_NOMEM)
  276.     ENDIF
  277.     StrAdd(work,work2,ALL)
  278.     IF quietflag=NIL
  279.         WriteF('Copying \s TO \s\n',work,dest)
  280.     ENDIF
  281.     CtrlC()
  282.     IF(filesize:=FileLength(work))>0
  283.         CtrlC()
  284.         IF (workfh:=Open(work,MODE_OLDFILE))
  285.             CtrlC()
  286.             IF (destfh:=Open(dest,MODE_NEWFILE))
  287.                 WHILE cont=FALSE
  288.                     CtrlC()
  289.                     numbytes:=Read(workfh,buffer,buffersize)
  290.                     IF numbytes=-1
  291.                         Raise(ERR_DOS)
  292.                     ELSE
  293.                         IF numbytes=0
  294.                             cont:=TRUE
  295.                         ELSE
  296.                             numbytes2:=Write(destfh,buffer,numbytes)
  297.                             IF numbytes<buffersize
  298.                                 cont:=TRUE
  299.                             ENDIF
  300.                         ENDIF
  301.                     ENDIF
  302.                 ENDWHILE
  303.             ELSE
  304.                 Raise(ERR_DOS)
  305.             ENDIF
  306.         ELSE
  307.             Raise(ERR_DOS)
  308.         ENDIF
  309.     ELSE
  310.         Raise(ERR_DOS)
  311.     ENDIF
  312.     IF (destfh) THEN Close(destfh);destfh:=NIL
  313.     IF (workfh) THEN Close(workfh);workfh:=NIL
  314.     IF cloneflag
  315.         IF (SetFileDate(dest,datestamp))=FALSE THEN Raise(ERR_DOS)
  316.         IF (SetComment(dest,comment))=FALSE THEN Raise(ERR_DOS)
  317.  
  318.                                     /* V V V -ALWAYS a good idea!!! */
  319.         IF (SetProtection(dest,condom))=FALSE THEN Raise(ERR_DOS)
  320.     ENDIF
  321.  
  322. ENDPROC
  323.  
  324. PROC instructions()
  325.     IF quietflag=NIL
  326.         WriteF('\nRandomcopy  version .90  December 28, 1993.\n')
  327.         WriteF('This program is copyright ®1993 by Chad Randall\n')
  328.         WriteF('and may be freely distributed.\n')
  329.         WriteF('\n')
  330.         WriteF('  EMAIL:crandall@garnet.msen.com\n')
  331.         WriteF(' USNAIL:Chad Randall, 229 S.Washington St.,\n')
  332.         WriteF('        Manchester, Michigan 48158-9680, USA.\n')
  333.         WriteF('\n')
  334.     ENDIF
  335. ENDPROC
  336.  
  337. CHAR '$VER: randomcopy 0.90 © 1993 Chad Randall (28.12.93)',0
  338.